package com.jeecms.wicool.dao.user.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.user.UserCouponDao;
import com.jeecms.wicool.entity.user.UserCoupon;

@Repository
public class UserCouponDaoImpl extends HibernateBaseDao<UserCoupon, Integer> implements
UserCouponDao {

	@Override
	protected Class<UserCoupon> getEntityClass() {
		// TODO Auto-generated method stub
		return UserCoupon.class;
	}

	@Override
	public Pagination getPage(Integer userId,Integer couponType,String mobile,String couponName,Integer couponSource,Short status, int pageNo, 
			int pageSize, CmsUser user) {
		Finder f = Finder.create("select bean from UserCoupon bean where 1=1 ");
		if (userId != null) {
			f.append(" and bean.user.id = :userId");
			f.setParam("userId", userId);
		}if (couponType != null) {
			f.append(" and bean.couponType = :couponType");
			f.setParam("couponType", couponType);
		}
		if (!StringUtils.isBlank(couponName)) {
			f.append(" and bean.coupon.couponName = :couponName");
			f.setParam("couponName", couponName);
		}if (couponSource != null) {
			f.append(" and bean.couponSource = :couponSource");
			f.setParam("couponSource", couponSource);
		}if (status != null) {
			f.append(" and bean.status = :status");
			f.setParam("status", status);
		}
		if(StringUtils.isNotBlank(mobile)){
			f.append(" and bean.user.mobile = "+mobile);
		}
		f.append(" order by bean.createTime desc");
		return find(f, pageNo, pageSize);
	}

	@Override
	public UserCoupon findById(Integer id) {
		UserCoupon entity = get(id);
		return entity;
	}

	@Override
	public UserCoupon save(UserCoupon bean) {
		getSession().save(bean);
		return bean;
	}

	@Override
	public UserCoupon deleteById(Integer id) {
		UserCoupon entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

}